package com.wrench.elevator.tida.controller;

import java.util.List;
import java.util.Map;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.wrench.elevator.tida.domain.wuye.WebUIFormatData;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

import com.alibaba.fastjson.JSON;
import com.wrench.elevator.tida.constant.Result;
import com.wrench.elevator.tida.domain.Page;
import com.wrench.elevator.tida.exception.WrenchException;
import com.wrench.elevator.tida.node.NodeProvince;
import com.wrench.elevator.tida.service.WYZHService;
import com.wrench.elevator.tida.util.WrenchUtil;

@Controller
@RequestMapping("/wuye_account")
public class WuyeAccountController
{
    private static final Logger logger = LoggerFactory.getLogger(WuyeAccountController.class);
    
    @Resource
    private WYZHService wYZHService;


    @RequestMapping("/getWholeRegion")
    public void  getWholeRegion(HttpServletRequest request, HttpServletResponse response, ModelMap map){
        logger.debug("==>**********enter in getWholeRegion(HttpServletRequest request, HttpServletResponse response, ModelMap map)**********");
        String wuye_id=WrenchUtil.getPara(request, "wuye_id");
        List<WebUIFormatData> data=null;
        WrenchException error=null;
        try {
            data=wYZHService.getWholeRegion(wuye_id);
        } catch (WrenchException e) {
           error=e;
        }
        if (WrenchUtil.hasNull(error))
        {
            map.put(Result.RESULT_CODE, Result.SUCCESS_CODE);
            map.put(Result.RESULT_MESSAGE, Result.SUCCESS_MESSAGE);
            map.put("data", data);

        }
        else
        {
            map.clear();
            map.put(Result.RESULT_CODE, error.getResultCode());
            map.put(Result.RESULT_MESSAGE, error.getResultMessage());
        }
        logger.debug("==> response = {}", JSON.toJSON(map));
        WrenchUtil.responseMap(response, map);
        map = null;
        logger.debug("==>**********exit out getWholeRegion(HttpServletRequest request, HttpServletResponse response, ModelMap map)**********");

    }



    @RequestMapping("/getSSQID")
    public void getSSQID(HttpServletRequest request, HttpServletResponse response, ModelMap map){
        logger.debug("==>**********enter in getSSQID(HttpServletRequest request, HttpServletResponse response, ModelMap map)**********");
        String id=WrenchUtil.getPara(request, "id");
        String type=WrenchUtil.getPara(request,"name");
        Map<String,String> ssqId=null;
        WrenchException error=null;
        try {
			ssqId=wYZHService.getSSQID(id,type);
		} catch (WrenchException e) {
			// TODO Auto-generated catch block
			error=e;
		}
        if (WrenchUtil.hasNull(error))
        {
          map.put(Result.RESULT_CODE, Result.SUCCESS_CODE);
          map.put(Result.RESULT_MESSAGE, Result.SUCCESS_MESSAGE);
          map.put("ssqId", ssqId);
            
        }
        else
        {
            map.clear();
            map.put(Result.RESULT_CODE, error.getResultCode());
            map.put(Result.RESULT_MESSAGE, error.getResultMessage());
        }
        logger.debug("==> response = {}", JSON.toJSON(map));
        WrenchUtil.responseMap(response, map);
        map = null;
        logger.debug("==>**********exit out getSSQID(HttpServletRequest request, HttpServletResponse response, ModelMap map)**********");

    }
    
    @RequestMapping("/getAllNodesBuWuye_id")
    public void getAllNodesBuWuye_id(HttpServletRequest request, HttpServletResponse response, ModelMap map)
    {
        logger.debug("==>**********enter in getAllNodesBuWuye_id(HttpServletRequest request, HttpServletResponse response, ModelMap map)**********");
        String user_id = WrenchUtil.getPara(request, "user_id");
        logger.debug("==> params : user_id = {} ", user_id);
        WrenchException error = null;
        List<NodeProvince> nodeProvinces = null;
        try
        {
            nodeProvinces = wYZHService.getAllNodesBuWuye_id(user_id);
        }
        catch (WrenchException e)
        {
            error = e;
        }
        if (WrenchUtil.hasNull(error))
        {
            map.clear();
            map.put(Result.RESULT_CODE, Result.SUCCESS_CODE);
            map.put(Result.RESULT_MESSAGE, Result.SUCCESS_MESSAGE);
            map.put("message", "queryWuYeUser  success");
            map.put("nodes", nodeProvinces);
            
        }
        else
        {
            map.clear();
            map.put(Result.RESULT_CODE, error.getResultCode());
            map.put(Result.RESULT_MESSAGE, error.getResultMessage());
        }
        logger.debug("==> response = {}", JSON.toJSON(map));
        WrenchUtil.responseMap(response, map);
        map = null;
        
        logger.debug("==>**********exit out getAllNodesBuWuye_id(HttpServletRequest request, HttpServletResponse response, ModelMap map)**********");
        
    }
    
    @RequestMapping(value = "/bindWuYeUserManage")
    public void bindWuYeUserManage(HttpServletRequest request, HttpServletResponse response, ModelMap map)
    {
        logger.debug("==>**********enter in bindWuYeUserManage(HttpServletRequest request, HttpServletResponse response, ModelMap map)**********");
        for(String s:request.getParameterMap().keySet())
            logger.debug("====> param : -----------> {}",s);
        String user_id = WrenchUtil.getPara(request, "user_id");
        String[] province_id = WrenchUtil.getParas(request, "province_id");
        String[] city_id = WrenchUtil.getParas(request, "city_id");
        String[] district_id = WrenchUtil.getParas(request, "district_id");
        String[] building_id = WrenchUtil.getParas(request, "building_id");
        
        logger.debug("==> params : user_id = {} , province_id = {} , city_id = {} , district_id = {} , building_id = {} ,",
            user_id,
            province_id,
            city_id,
            district_id,
            building_id);
        WrenchException error = null;
        try
        {
            wYZHService.bindWuYeUserManage(user_id, province_id, city_id, district_id, building_id, map);
        }
        catch (WrenchException e)
        {
            error = e;
        }
        if (WrenchUtil.hasNull(error))
        {
            Object flag = map.get("flag");
            map.clear();
            if ("SUCCESS".equals(flag))
            {
                map.put(Result.RESULT_CODE, Result.SUCCESS_CODE);
                map.put(Result.RESULT_MESSAGE, Result.SUCCESS_MESSAGE);
                map.put("message", "bindWuYeUserManage success");
            }
            
            else
            {
                map.put(Result.RESULT_CODE, Result.OTHER_ERROR_CODE);
                map.put(Result.RESULT_MESSAGE, Result.OTHER_ERROR_MESSAGE);
            }
            
        }
        else
        {
            map.clear();
            map.put(Result.RESULT_CODE, error.getResultCode());
            map.put(Result.RESULT_MESSAGE, error.getResultMessage());
        }
        logger.debug("==> response = {}", JSON.toJSON(map));
        WrenchUtil.responseMap(response, map);
        map = null;
        logger.debug("==>**********exit out bindWuYeUserManage(HttpServletRequest request, HttpServletResponse response, ModelMap map)**********");
    }
    
    @RequestMapping(value = "/queryWuYeUser", method = RequestMethod.GET)
    public void queryWuYeUser(HttpServletRequest request, HttpServletResponse response, ModelMap map)
    {
        logger.debug("==>**********enter in queryWuYeUser(HttpServletRequest request, HttpServletResponse response, ModelMap map)**********");
        String name = WrenchUtil.getPara(request, "name");
        String current_page = WrenchUtil.getPara(request, "current_page");
        String limit = WrenchUtil.getPara(request, "limit");
        Page page = null;
        WrenchException error = null;
        try
        {
            page = wYZHService.queryWuYeUser(name, current_page, limit);
        }
        catch (WrenchException e)
        {
            error = e;
        }
        
        if (WrenchUtil.hasNull(error))
        {
            map.clear();
            map.put(Result.RESULT_CODE, Result.SUCCESS_CODE);
            map.put(Result.RESULT_MESSAGE, Result.SUCCESS_MESSAGE);
            map.put("message", "queryWuYeUser  success");
            map.put("page", page);
            
        }
        else
        {
            map.clear();
            map.put(Result.RESULT_CODE, error.getResultCode());
            map.put(Result.RESULT_MESSAGE, error.getResultMessage());
        }
        logger.debug("==> response = {}", JSON.toJSON(map));
        WrenchUtil.responseMap(response, map);
        map = null;
        
        logger.debug("==>**********exit out queryWuYeUser(HttpServletRequest request, HttpServletResponse response, ModelMap map)**********");
    }
    
    @RequestMapping(value = "/delWuYeUser", method = RequestMethod.POST)
    public void delWuYeUser(HttpServletRequest request, HttpServletResponse response, ModelMap map)
    {
        logger.debug("==>**********enter in delWuYeUser(HttpServletRequest request, HttpServletResponse response, ModelMap map)**********");
        String user_id = WrenchUtil.getPara(request, "user_id");
        logger.debug("==> params : user_id = {} ", user_id);
        WrenchException error = null;
        try
        {
            wYZHService.delWuYeUser(user_id, map);
        }
        catch (WrenchException e)
        {
            error = e;
        }
        if (WrenchUtil.hasNull(error))
        {
            Object flag = map.get("flag");
            map.clear();
            if ("SUCCESS".equals(flag))
            {
                map.put(Result.RESULT_CODE, Result.SUCCESS_CODE);
                map.put(Result.RESULT_MESSAGE, Result.SUCCESS_MESSAGE);
                map.put("message", "delWuYeUser success");
            }
            
            else
            {
                map.put(Result.RESULT_CODE, Result.OTHER_ERROR_CODE);
                map.put(Result.RESULT_MESSAGE, Result.OTHER_ERROR_MESSAGE);
            }
            
        }
        else
        {
            map.clear();
            map.put(Result.RESULT_CODE, error.getResultCode());
            map.put(Result.RESULT_MESSAGE, error.getResultMessage());
        }
        logger.debug("==> response = {}", JSON.toJSON(map));
        WrenchUtil.responseMap(response, map);
        map = null;
        logger.debug("==>**********exit out delWuYeUser(HttpServletRequest request, HttpServletResponse response, ModelMap map)**********");
    }
    
    @RequestMapping(value = "/addWuYeUser", method = RequestMethod.POST)
    public void addWuYeUser(HttpServletRequest request, HttpServletResponse response, ModelMap map)
    {
        logger.debug("==>**********enter in addWuYeUser(HttpServletRequest request, HttpServletResponse response, ModelMap map)**********");
        String company_name = WrenchUtil.getPara(request, "company_name");
        String user_name = WrenchUtil.getPara(request, "user_name");
        String user_tel = WrenchUtil.getPara(request, "user_tel");
        String superior_id = WrenchUtil.getPara(request, "superior_id");
        String role_id = WrenchUtil.getPara(request, "role_id");
        logger.debug("==> params :  company_name = {} , user_name = {} , user_tel = {} , superior_id = {} , role_id = {} ",
            company_name,
            user_name,
            user_tel,
            superior_id,
            role_id);
        
        WrenchException error = null;
        try
        {
            wYZHService.addWuYeUser(company_name, user_name, user_tel, superior_id, role_id, map);
        }
        catch (WrenchException e)
        {
            error = e;
        }
        
        if (WrenchUtil.hasNull(error))
        {
            Object flag = map.get("flag");
            map.clear();
            if ("SUCCESS".equals(flag))
            {
                map.put(Result.RESULT_CODE, Result.SUCCESS_CODE);
                map.put(Result.RESULT_MESSAGE, Result.SUCCESS_MESSAGE);
                map.put("message", "addWuYeUser success");
            }
            else if ("EXIST".equals(flag))
            {
                map.put(Result.RESULT_CODE, Result.WUYE_USER_IS_EXIST_CODE);
                map.put(Result.RESULT_MESSAGE, Result.WUYE_USER_IS_EXIST_MESSAGE);
            }
            else
            {
                map.put(Result.RESULT_CODE, Result.OTHER_ERROR_CODE);
                map.put(Result.RESULT_MESSAGE, Result.OTHER_ERROR_MESSAGE);
            }
            
        }
        else
        {
            map.clear();
            map.put(Result.RESULT_CODE, error.getResultCode());
            map.put(Result.RESULT_MESSAGE, error.getResultMessage());
        }
        logger.debug("==> response = {}", JSON.toJSON(map));
        WrenchUtil.responseMap(response, map);
        map = null;
        
        logger.debug("==>**********exit out addWuYeUser(HttpServletRequest request, HttpServletResponse response, ModelMap map)**********");
    }
    
    @RequestMapping(value = "/updateWuYeUser", method = RequestMethod.POST)
    public void updateWuYeUser(HttpServletRequest request, HttpServletResponse response, ModelMap map)
    {
        logger.debug("==>**********enter in updateWuYeUser(HttpServletRequest request, HttpServletResponse response, ModelMap map)**********");
        String company_name = WrenchUtil.getPara(request, "company_name");
        String user_name = WrenchUtil.getPara(request, "user_name");
        String user_tel = WrenchUtil.getPara(request, "user_tel");
        String superior_id = WrenchUtil.getPara(request, "superior_id");
        String role_id = WrenchUtil.getPara(request, "role_id");
        String user_id = WrenchUtil.getPara(request, "user_id");
        logger.debug("==> params : user_id = {},  company_name = {} , user_name = {} , user_tel = {} , superior_id = {} , role_id = {} ",
            user_id,
            company_name,
            user_name,
            user_tel,
            superior_id,
            role_id);
        
        WrenchException error = null;
        try
        {
            wYZHService.updateWuYeUser(user_id, company_name, user_name, user_tel, superior_id, role_id, map);
        }
        catch (WrenchException e)
        {
            error = e;
        }
        
        if (WrenchUtil.hasNull(error))
        {
            Object flag = map.get("flag");
            map.clear();
            if ("SUCCESS".equals(flag))
            {
                map.put(Result.RESULT_CODE, Result.SUCCESS_CODE);
                map.put(Result.RESULT_MESSAGE, Result.SUCCESS_MESSAGE);
                map.put("message", "updateWuYeUser success");
            }
            else if ("NOTEXIST".equals(flag))
            {
                map.put(Result.RESULT_CODE, Result.WUYE_USER_IS_NOT_EXIST_CODE);
                map.put(Result.RESULT_MESSAGE, Result.WUYE_USER_IS_NOT_EXIST_MESSAGE);
            }
            else
            {
                map.put(Result.RESULT_CODE, Result.OTHER_ERROR_CODE);
                map.put(Result.RESULT_MESSAGE, Result.OTHER_ERROR_MESSAGE);
            }
            
        }
        else
        {
            map.clear();
            map.put(Result.RESULT_CODE, error.getResultCode());
            map.put(Result.RESULT_MESSAGE, error.getResultMessage());
        }
        logger.debug("==> response = {}", JSON.toJSON(map));
        WrenchUtil.responseMap(response, map);
        map = null;
        
        logger.debug("==>**********exit out updateWuYeUser(HttpServletRequest request, HttpServletResponse response, ModelMap map)**********");
    }
    
}
